<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            position: relative;
        }
        .box{
            display: none;
            width: 300px;
            height: 300px;
            margin: 200px;
            background-color: #f5f5f5;
            left:0;
            top: 0;
            z-index: 999;
            position: absolute;
        }
        .title{
            height: 100px;
            background-color: aqua;

        }
        .bg{
            display: none;
            position: absolute;
            z-index: 99;
            background: rgba(220,220,220, .5);
            height: 100vh;
            width: 100%;
        }
    </style>
</head>
<body>
    <div class="bg"></div>
    <button class="clickbtn">点击弹出提示框</button>
    <div class="box">
        <div class="title">这是一个title</div>
        <div class="btngrp">
            <button>确认</button>
            <button id="shutdown">取消</button>
        </div>
    </div>
    <script>
        //1.获取元素
        var btn = document.querySelector('.clickbtn')
        var box = document.querySelector('.box')
        var bg = document.querySelector('.bg')
        var shutdown = document.getElementById('shutdown')
        var title = document.querySelector('.title')
        // 2.点击btn 显示遮罩层 和 box弹窗
        btn.addEventListener('click',function(){
            box.style.display = 'block'
            bg.style.display = 'block'
        })
        //3. 点击弹窗中的关闭按钮再关闭弹窗和背景
        shutdown.addEventListener('click',function(){
            box.style.display = 'none'
            bg.style.display = 'none'
        })
        //4.点击title时获取鼠标在title内的坐标，然后再用pageX,pageY减去鼠标的坐标 求得box距离页面顶部跟左部的距离
        // 将计算的值赋值给box
        var x =null;
        var y =null;
        title.addEventListener('mousedown',function(e){
            // 鼠标点击时在title框中的坐标
            x = e.pageX - this.offsetLeft;
            y = e.pageY - this.offsetTop;
            // 点击后，再通过鼠标的mousemove事件实施计算box与页面 顶部 左部的距离
            function fn(e){
                var leftX = e.pageX - x;
                var topY = e.pageY - y;
                box.style.left = leftX + 'px'
                box.style.top = topY + 'px'
            } 
            document.addEventListener('mousemove',fn)
            //鼠标点击松开弹出后，再清除title对象上的监听事件
            //先给文本添加mouseup监听事件，然后再给document取消绑定mousemove事件
            document.addEventListener('mouseup',function(){
                document.removeEventListener('mousemove',fn)
            })
        })
    </script>
</body>
</html>